﻿using AutoMapper;
using CarAppDomain.CarType.Entities;
using CarAppDomain.CarType.IRepository;
using CarAppDomain.CarType.IService;
using CarAppDto.CarType.CarDtos;
using D.Util.UtilCache;
using D.UtilCore.Dtos;
using D.UtilCore.Utils;
using D.UtilSqlsugarOrm;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace CarAppDomain.CarType.Service
{
    public class CarService : BaseService<CarEntity>,ICarService
    {
        private readonly IMapper _mapper;
        private readonly ICacheService _cache;
        private readonly ICarEntityRepository _repository;

        /// <summary>
        /// 构造注入
        /// </summary>
        public CarService(IMapper mapper, ICacheService cache, ICarEntityRepository repository)
        {
            // 用于base方法
            base._baseDal = repository;
            this._repository = repository;
            _cache = cache;
            _mapper = mapper;
        }

        #region 增删改查


        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="reqModel"></param>
        /// <returns></returns>
        public async Task<ApiResult<int>> AddCarAsync(ReqAddCar reqModel)
        {
            //var exists = await _repository.ExistsAsync(m => m.CarTypeId == reqModel.CarTypeId || m.CarTypeVersionId == reqModel.CarTypeVersionId);
            //if (exists)
            //{
            //    return new ApiResult<int>() { Code = StatusCodeEnum.Waring, ErrorMsg = "重复" };
            //}
            var insertEntity = _mapper.Map<ReqAddCar,CarEntity>(reqModel);
            insertEntity.DeleteFlag = false;
            insertEntity.CreateTime = DateTime.Now;
            insertEntity.Id = Guid.NewGuid().ToString();
            var dbres = await _repository.AddAsync(insertEntity);
            return dbres.NormalResponse();
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public async Task<ApiResult<int>> DeleteCarAsync(List<string> ids)
        {
            var dbres = await _repository.DeleteAsync(m => ids.Contains(m.Id));
            return dbres.NormalResponse();
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="reqModel"></param>
        /// <returns></returns>
        public async Task<ApiResult<int>> UpdateCarAsync(ReqUpdateCar reqModel)
        {

            var updateEntity = await _repository.GetModelAsync(m => m.Id == reqModel.Id);
            if (updateEntity == null)
            {
                return new ApiResult<int>() { Code = StatusCodeEnum.Waring, ErrorMsg = "该信息不存在" };
            }
            var exists = await _repository.ExistsAsync(m =>m.Id!=reqModel.Id&&( m.Id == reqModel.Id || m.Id == reqModel.Id)); 
            if (exists)
            {
                return new ApiResult<int>() { Code = StatusCodeEnum.Waring, ErrorMsg = "名称或英文名称重复" };
            }

           
            updateEntity.UpdateTime = DateTime.Now;
            updateEntity.UpdateUser = reqModel.UpdateUser;

            var dbres = await _repository.UpdateAsync(updateEntity);
            return dbres.NormalResponse();
        }

        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="reqModel"></param>
        /// <returns></returns>
        public async Task<ApiResult<List<RepCar>>> QueryCarListAsync(ReqQueryCarList reqModel)
        {
            Expression<Func<CarEntity, bool>> where = null;
            if (!string.IsNullOrEmpty(reqModel.CarTypeVersionId))
            {
                where = where.And(m => m.CarTypeVersionId.Contains(reqModel.CarTypeVersionId));
            }

            if (!string.IsNullOrEmpty(reqModel.CarTypeVersionId))
            {
                where = where.And(m => m.CarTypeVersionId.Contains(reqModel.CarTypeVersionId));
            }

            var dbres = await _repository.GetListAsync(where);
            var res = _mapper.Map<List<CarEntity>, List<RepCar>>(dbres);
            return res.GetResponse();
        }

        /// <summary>
        /// 获取用户分页
        /// </summary>
        /// <param name="reqQueryAdminPage"></param>
        /// <returns></returns>
        public async Task<ApiResult<PageModel<RepCar>>> QueryCarPagesAsync(ReqQueryCarPage reqQueryPage)
        {
            Expression<Func<CarEntity, bool>> where = null;
            //if (!string.IsNullOrEmpty(reqQueryPage.Name))
            //{
            //    where = where.And(m => m.Name.Contains(reqQueryPage.Name));
            //}

            //if (!string.IsNullOrEmpty(reqQueryPage.EnglishName))
            //{
            //    where = where.And(m => m.EnglishName.Contains(reqQueryPage.EnglishName));
            //}
        

            var dbres = await _repository.GetPagesAsync(reqQueryPage.PageSize, reqQueryPage.PageNo, where, m => m.CreateTime);
            var res = new PageModel<RepCar>()
            {
                PageNo = dbres.PageNo,
                PageSize = dbres.PageSize,
                TotalCount = dbres.TotalCount,
                TotalPage = dbres.TotalPage,
                Data = _mapper.Map<List<CarEntity>, List<RepCar>>(dbres.Data)
            };
            return res.GetResponse();
        }

        /// <summary>
        /// 查询单个
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<ApiResult<RepCar>> GetCarAsync(string id)
        {
            var dbres = await _repository.GetModelAsync(m => m.Id == id);
            var res = _mapper.Map<CarEntity, RepCar>(dbres);
            return res.GetResponse();
        }

        #endregion
    }
}
